// pages/person/person.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: null,
    avatarUrl: '',
    name:'',
    commonFunctions: [
      {
        id: 1,
        name: '个人信息',
        icon: '/assets/icons/index-nav/per-info.png',
        path: '/pages/index/info-form/info-form'
      },
      { id: 2,
        name: '军事兴趣社团',
        icon: '/assets/icons/index-nav/precollect.png',
        path: '/pages/index/precollect/precollect'
      },
      { id: 3,
        name: '征兵小助手',
        icon: '/assets/icons/index-nav/AI.png',
        path: '/pages/index/AI/AI'
      },
      { id: 4,
        name: '征兵流程',
        icon: '/assets/icons/index-nav/progress.png',
        path: '/pages/index/progress/progress'
      }
    ],
    otherFunctions: [
      { id: 1, name: '知识竞答', icon: '/assets/icons/person-icon/quiz.png' ,path: '/pages/person/Quiz-index/Quiz-index'},
      { id: 2, name: '国防教育馆', icon: '/assets/icons/person-icon/reservation.png',path: '/pages/person/Exhibition/Exhibition'},
      { id: 3, name: '意见反馈', icon: '/assets/icons/person-icon/Feedback.png',path: '/pages/person/Feedback/Feedback'},
      { id: 4, name: '优秀个人展示', icon: '/assets/icons/person-icon/Previous-exce.png',path: '/pages/person/Previous-excellent/Previous-excellent'}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkLoginStatus();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkLoginStatus();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token')
    const userInfo = wx.getStorageSync('userInfo')
    if (token && userInfo) {
      this.setData({
        isLogin: true,
        userInfo: userInfo,
        avatarUrl: wx.getStorageSync('avatarUrl'),
        name: wx.getStorageSync('name')
      })
    } else {
      this.setData({
        isLoggedIn: false,
        userInfo: null,
      })
    }
  },

  goToLogin() {
    wx.navigateTo({
      url: '/pages/person/login/login'
    });
  },

  goToEditProfile() {
    wx.navigateTo({
      url: '/pages/person/edit-profile/edit-profile'
    });
  },

  previewAvatar() {
    if (this.data.userInfo && this.data.userInfo.avatarUrl) {
      wx.previewImage({
        urls: [this.data.userInfo.avatarUrl]
      });
    }
  },

  // 处理退出登录
  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清空本地缓存
          wx.clearStorageSync()
          //断开socket连接
          const socketPath = this.globalData?.ws.url;
          wx.closeSocket({ socketPath }); // 必须传入正确的 socketPath
          app.globalData.ws = null;
          //清除app.js中的缓存
          app.globalData.chatList = [];
          app.globalData.unreadCount = 0;
          app.globalData.reconnectSocketCount = 1;
          //清除徽标
          wx.setTabBarBadge({
            index: 2,
            text: 0
          });

          // 更新状态
          this.setData({
            isLogin: false,
            userInfo: null,
            avatarUrl: null,
            name:null,
          })

          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
          console.log('over')

          // 可以选择跳转到首页
          setTimeout(() => {
            wx.switchTab({
              //url: '/pages/index/index'
            })
          }, 1500)
        }
      }
    })
  },

  handleFunctionClick(e) {
    const functionItem = e.currentTarget.dataset.function;
    console.log('clicked function:', functionItem);

    if (functionItem && functionItem.path) {
      wx.navigateTo({
        url: functionItem.path,  // 直接使用路径，不需要模板字符串
        fail: (err) => {
          console.error('navigation failed:', err);
          wx.showToast({
            title: '页面跳转失败',
            icon: 'none'
          });
        }
      });
    } else {
      console.warn('功能项未配置路径', functionItem);
      wx.showToast({
        title: '功能暂未开放',
        icon: 'none'
      });
    }
  }

})